home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / progutil / iostream.zoo / src / fvwrite.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  4.5 KB  |  180 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Modified for GNU iostream by Per Bothner 1991.
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "%W% (Berkeley) %G%";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include "ioprivat.h"
  25. #include <string.h>
  26. #include "fvwrite.h"
  27.  
  28. /*
  29.  * Write some memory regions.  Return zero on success, EOF on error.
  30.  *
  31.  * This routine is large and unsightly, but most of the ugliness due
  32.  * to the three different kinds of output buffering is handled here.
  33.  */
  34. int __sbvwrite(register streambuf *fp, register struct __suio *uio)
  35. {
  36.     register size_t len;
  37.     register char *p;
  38.     register struct __siov *iov;
  39.     register int w;
  40.  
  41.     if ((len = uio->uio_resid) == 0)
  42.         return (0);
  43.     /* make sure we can write */
  44.     if (!(fp->_flags & _S_CAN_WRITE))
  45.         return (EOF);
  46.  
  47. #define    MIN(a, b) ((a) < (b) ? (a) : (b))
  48. #define    COPY(n)      (void) memmove((void *)fp->_p, (void *)p, (size_t)(n));
  49.  
  50.     iov = uio->uio_iov;
  51.     len = 0;
  52. #define GETIOV(extra_work) \
  53.     while (len == 0) { \
  54.         extra_work; \
  55.         p = iov->iov_base; \
  56.         len = iov->iov_len; \
  57.         iov++; \
  58.     }
  59. #if 0
  60.     if (fp->_flags & __SNBF) {
  61. #endif
  62.         /*
  63.          * Unbuffered: write up to BUFSIZ bytes at a time.
  64.          */
  65.         do {
  66.             GETIOV(;);
  67. #if 1
  68.             w = MIN(len, BUFSIZ);
  69.             int iw = w;
  70.             while (--iw >= 0) fp->sputc(*p++);
  71. #else
  72.             w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
  73. #endif
  74.             if (w <= 0)
  75.                 goto err;
  76.             p += w;
  77.             len -= w;
  78.         } while ((uio->uio_resid -= w) != 0);
  79. #if 0
  80.     } else if ((fp->_flags & __SLBF) == 0) {
  81.         /*
  82.          * Fully buffered: fill partially full buffer, if any,
  83.          * and then flush.  If there is no partial buffer, write
  84.          * one _bf._size byte chunk directly (without copying).
  85.          *
  86.          * String output is a special case: write as many bytes
  87.          * as fit, but pretend we wrote everything.  This makes
  88.          * snprintf() return the number of bytes needed, rather
  89.          * than the number used, and avoids its write function
  90.          * (so that the write function can be invalid).
  91.          */
  92.         do {
  93.             GETIOV(;);
  94.             w = fp->_w;
  95.             if (fp->_flags & __SSTR) {
  96.                 if (len < w)
  97.                     w = len;
  98.                 COPY(w);    /* copy MIN(fp->_w,len), */
  99.                 fp->_w -= w;
  100.                 fp->_p += w;
  101.                 w = len;    /* but pretend copied all */
  102.             } else if (fp->_p > fp->_bf._base && len > w) {
  103.                 /* fill and flush */
  104.                 COPY(w);
  105.                 /* fp->_w -= w; */ /* unneeded */
  106.                 fp->_p += w;
  107.                 if (fflush(fp))
  108.                     goto err;
  109.             } else if (len >= (w = fp->_bf._size)) {
  110.                 /* write directly */
  111.                 w = (*fp->_write)(fp->_cookie, p, w);
  112.                 if (w <= 0)
  113.                     goto err;
  114.             } else {
  115.                 /* fill and done */
  116.                 w = len;
  117.                 COPY(w);
  118.                 fp->_w -= w;
  119.                 fp->_p += w;
  120.             }
  121.             p += w;
  122.             len -= w;
  123.         } while ((uio->uio_resid -= w) != 0);
  124.     } else {
  125.         /*
  126.          * Line buffered: like fully buffered, but we
  127.          * must check for newlines.  Compute the distance
  128.          * to the first newline (including the newline),
  129.          * or `infinity' if there is none, then pretend
  130.          * that the amount to write is MIN(len,nldist).
  131.          */
  132.             int nlknown, nldist;
  133.         char *nl;
  134.         register int s;
  135.         nlknown = 0;
  136.         do {
  137.             GETIOV(nlknown = 0);
  138.             if (!nlknown) {
  139.                 nl = memchr((void *)p, '\n', len);
  140.                 nldist = nl ? nl + 1 - p : len + 1;
  141.                 nlknown = 1;
  142.             }
  143.             s = MIN(len, nldist);
  144.             w = fp->_w + fp->_bf._size;
  145.             if (fp->_p > fp->_bf._base && s > w) {
  146.                 COPY(w);
  147.                 /* fp->_w -= w; */
  148.                 fp->_p += w;
  149.                 if (fflush(fp))
  150.                     goto err;
  151.             } else if (s >= (w = fp->_bf._size)) {
  152.                 w = (*fp->_write)(fp->_cookie, p, w);
  153.                 if (w <= 0)
  154.                      goto err;
  155.             } else {
  156.                 w = s;
  157.                 COPY(w);
  158.                 fp->_w -= w;
  159.                 fp->_p += w;
  160.             }
  161.             if ((nldist -= w) == 0) {
  162.                 /* copied the newline: flush and forget */
  163.                 if (fflush(fp))
  164.                     goto err;
  165.                 nlknown = 0;
  166.             }
  167.             p += w;
  168.             len -= w;
  169.         } while ((uio->uio_resid -= w) != 0);
  170.     }
  171. #endif
  172.     return (0);
  173.  
  174. err:
  175. #if 0
  176.     fp->_flags |= __SERR;
  177. #endif
  178.     return (EOF);
  179. }
  180.